package com.example.shoppingdemo.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.javaBean.Homebean;
import com.example.shoppingdemo.nslcommodity.CommodityAdapter;
import com.example.shoppingdemo.nslcommodity.CommodityModel;

import java.util.ArrayList;
import java.util.List;

public class HomeAdpter extends RecyclerView.Adapter<HomeAdpter.recy> {
    private List<CommodityModel>commodityModels;
    private onXiangQ xiangQ;

    public void setXiangQ(onXiangQ xiangQ) {
        this.xiangQ = xiangQ;
    }

    public void setCommodityModels(List<CommodityModel> commodityModels) {
        this.commodityModels = commodityModels;
    }
    private Context context;

    public HomeAdpter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public recy onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        return new recy
                (LayoutInflater.from(context)
                        .inflate(R.layout.list_home,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull HomeAdpter.recy holder, int position) {
        holder.textView.setText(commodityModels.get(position).getTitle());
        holder.textView2.setText("￥"+commodityModels.get(position).getMoney());
        Glide
                //显示在
                .with(context)
                //.asGif()指定加载图片类型
                //获取图片资源
                .load(commodityModels.get(position).getIcon())
                //加载中显示的图片
                .placeholder(R.mipmap.jiazai)
                //错误后显示的图片
                .error(R.mipmap.shibai)
                //Glide方法用来控制加载缓冲
                //ALL (默认值缓存所有图像)
                //NONE (什么都不缓存)
                //SOURCE (仅仅只缓存原来的全分辨率的图像)
                //RESULT (仅仅缓存最终的图像，即降低分辨率后的图像)。
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                //指定图片尺寸100*100像素，手动设置图片宽高，节省了内存的开销
                //.override(100,100)
                //将图片放入的地方
                .into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                xiangQ.onxiangqing(holder.getAdapterPosition());
            }
        });
    }
    @Override
    public int getItemCount() {
        return null==commodityModels?0:commodityModels.size();
    }

    class recy extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView textView;
        private TextView textView2;
        public recy(@NonNull  View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.list_home_img);
            textView=itemView.findViewById(R.id.title_name);
            textView2=itemView.findViewById(R.id.list_home_price);
        }
}
public interface onXiangQ{
        void onxiangqing(int position);
}
}
